package com.shuqi.activity.adapter;

import java.util.List;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.activity.BookSearchParentActivity;
import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

/**
 * 搜索主页热搜排行列表适配器
 * 
 * @author linyuren
 * 
 */
public class BookSearchRanklistAdapter extends BaseAdapter {

    /** 存储对象集合 */
    private List<BookInfo> searchLabelInfoData;

    private LayoutInflater inflater;

    /** 构造 */
    public BookSearchRanklistAdapter(BookSearchParentActivity context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setRanklistAdapter(List<BookInfo> data) {
        this.searchLabelInfoData = data;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return searchLabelInfoData == null ? 0 : searchLabelInfoData.size();
    }

    @Override
    public BookInfo getItem(int position) {
        return this.searchLabelInfoData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        ViewHolder holder = null;
        if (convertView != null) {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.item_act_booksearch_ranklist, parent, false);
            holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.search_rank_img = (ImageView) view.findViewById(R.id.search_rank_img);
                holder.search_rank_topNum = (TextView) view.findViewById(R.id.search_rank_topNum);
                holder.search_rank_bookname =
                        (TextView) view.findViewById(R.id.search_rank_bookname);
                holder.search_rank_bookauthor =
                        (TextView) view.findViewById(R.id.search_rank_bookauthor);
                holder.search_rank_bookupda_chapter =
                        (TextView) view.findViewById(R.id.search_rank_bookupda_chapter);
                holder.search_rank_book_kind =
                        (TextView) view.findViewById(R.id.search_rank_book_kind);
                view.setTag(holder);
            }
        }

        holder.search_rank_img.setTag(position + "");
        BookInfo slInfo = null;
        if (!this.searchLabelInfoData.isEmpty()) {
            slInfo = this.searchLabelInfoData.get(position);
        }
        if (slInfo != null) {
            holder.search_rank_bookname.setText(slInfo.getBookName());
            holder.search_rank_bookauthor.setText(slInfo.getBookAuthorName());
            holder.search_rank_bookupda_chapter.setText(slInfo.getBookIntro());
            holder.search_rank_book_kind.setText(slInfo.getBookType());

            // 异步加载图片
            SqImageManager.getImageManager().displayImage(slInfo.getBookCoverImgUrl(),
                    holder.search_rank_img, SqImageManager.IMG_COVER);
            if (!"".equals(slInfo.getSourceId()) && slInfo.getSourceId() != null) {
                if ("1".equals(slInfo.getSourceId())) {
                    holder.search_rank_topNum.setBackgroundColor(Color.parseColor("#ff5b5b"));
                } else if ("2".equals(slInfo.getSourceId())) {
                    holder.search_rank_topNum.setBackgroundColor(Color.parseColor("#ff8244"));
                } else if ("3".equals(slInfo.getSourceId())) {
                    holder.search_rank_topNum.setBackgroundColor(Color.parseColor("#14be7d"));
                }
                holder.search_rank_topNum.setText(slInfo.getSourceId());
                holder.search_rank_topNum.setVisibility(View.VISIBLE);
            } else {
                holder.search_rank_topNum.setVisibility(View.GONE);
            }
        }
        return view;
    }

    private static class ViewHolder {
        public ImageView search_rank_img; // 书logo
        public TextView search_rank_topNum; // 书排行名称
        public TextView search_rank_bookname;// 名称
        public TextView search_rank_bookauthor;// 作者
        public TextView search_rank_bookupda_chapter;// 更新至章节
        public TextView search_rank_book_kind;// 书籍类型
    }

}
